package com.satis.compose.get.widget

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.contentColorFor
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.satis.compose.get.navigator.GetPageController
import com.satis.compose.get.navigator.LocalPageController

/**
 * @author sunshaobei on 2022/9/8
 */

@Composable
fun GetPage(
    @SuppressLint("ModifierParameter") modifier: Modifier = Modifier.fillMaxSize(),
    statusBarColor: Color = Color.Transparent,
    navigationBarColor: Color = MaterialTheme.colors.primaryVariant,
    darkIcons: Boolean = false,
    shape: Shape = RectangleShape,
    color: Color = MaterialTheme.colors.background,
    contentColor: Color = contentColorFor(color),
    border: BorderStroke? = null,
    elevation: Dp = 0.dp,
    onCreate: (() -> Unit)? = null,
    onStart: (() -> Unit)? = null,
    onResume: (() -> Unit)? = null,
    onPause: (() -> Unit)? = null,
    onStop: (() -> Unit)? = null,
    onDestroy: (() -> Unit)? = null,
    topBar: (@Composable () -> Unit)? = null,
    bottomBar: (@Composable () -> Unit)? = null,
    content: @Composable GetPageController.() -> Unit
) {
    val lifecycleOwner = LocalLifecycleOwner.current
    val pageController = LocalPageController.current
    val route = pageController.route()
    DisposableEffect(lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> {
                    Log.e("Get:$route","ON_CREATE")
                    onCreate?.invoke()
                }
                Lifecycle.Event.ON_START -> {
                    Log.e("Get:$route","ON_START")
                    onStart?.invoke()
                }
                Lifecycle.Event.ON_RESUME -> {
                    Log.e("Get:$route","ON_RESUME")
                    onResume?.invoke()
                }
                Lifecycle.Event.ON_PAUSE -> {
                    Log.e("Get:$route","ON_PAUSE")
                    onPause?.invoke()
                }
                Lifecycle.Event.ON_STOP -> {
                    Log.e("Get:$route","ON_STOP")
                    onStop?.invoke()
                }
                Lifecycle.Event.ON_DESTROY -> {
                    Log.e("Get:$route","ON_DESTROY")
                    onDestroy?.invoke()
                }
                else -> {}
            }
        }
        lifecycleOwner.lifecycle.addObserver(observer)
        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }

    val statusBarInfo = StatusBarInfo(color = statusBarColor, darkIcons = darkIcons)
    val systemUiController = rememberSystemUiController()
    SideEffect {
        systemUiController.setStatusBarColor(statusBarColor)
        systemUiController.setNavigationBarColor(navigationBarColor)
        systemUiController.systemBarsDarkContentEnabled = darkIcons
    }
    CompositionLocalProvider(LocalStatusBarInfo provides statusBarInfo) {
        Scaffold(topBar = {topBar?.invoke()}, bottomBar = {bottomBar?.invoke()}) {
            Surface(
                modifier = Modifier.padding(it).navigationBarsPadding().then(modifier),
                content = {pageController.content()},
                shape = shape,
                contentColor = contentColor,
                color = color,
                border = border,
                elevation = elevation
            )
        }

    }
}

val LocalStatusBarInfo = compositionLocalOf<StatusBarInfo> { error("StatusBarInfo is null") }

data class StatusBarInfo(val color: Color, val darkIcons: Boolean)
